﻿using Microsoft.Owin.Security.DataProtection;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Owin.Security.Custom.DataProtection
{
    public class AesDataProtector : IDataProtector
    {
        private string _key = "";
        private string _IV = "";

        public AesDataProtector()
        {
            NameValueCollection configSection = (NameValueCollection)ConfigurationManager.GetSection("AesDataProtectorConfig");
            _key = configSection["key"].ToString();
            _IV = configSection["iv"].ToString();
        }
       
        public byte[] Protect(byte[] userData)
        {
            byte[] encrypted;
            //string text = System.Text.Encoding.Default.GetString(userData);
            string text = Convert.ToBase64String(userData);

            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Key = Convert.FromBase64String(_key);
                aesAlg.IV = Convert.FromBase64String(_IV);

                // Create a decrytor to perform the stream transform.
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for encryption. 
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                           
                            //Write all data to the stream.
                            swEncrypt.Write(text);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }

            return encrypted;
        }

        public byte[] Unprotect(byte[] protectedData)
        {
            string plaintext = null;

            // Create an AesCryptoServiceProvider object 
            // with the specified key and IV. 
            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Key = Convert.FromBase64String(_key);
                aesAlg.IV = Convert.FromBase64String(_IV);

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for decryption. 
                using (MemoryStream msDecrypt = new MemoryStream(protectedData))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {

                            // Read the decrypted bytes from the decrypting stream 
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }

            //byte[] bytes = new byte[plaintext.Length * sizeof(char)];
            //System.Buffer.BlockCopy(plaintext.ToCharArray(), 0, bytes, 0, bytes.Length);

            return Convert.FromBase64String(plaintext);
        }
    }
}
